package com.hainatong.shiphelper.login.view;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;

import com.base.common.netBeanPackage.BaseResponse;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.KeyboardUtils;
import com.base.common.utils.OnClickCheckedUtil;
import com.base.common.utils.UIUtils;
import com.base.common.view.base.BaseFragment;
import com.base.common.viewmodel.BaseViewObserver;
import com.blankj.utilcode.util.LogUtils;
import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.api.AppConstants;
import com.hainatong.shiphelper.api.PublicValues;
import com.hainatong.shiphelper.databinding.FragmentHandlePwdBinding;
import com.hainatong.shiphelper.login.vm.LoginVM;
import com.jeremyliao.liveeventbus.LiveEventBus;

public class HandlePwdFragment extends BaseFragment<FragmentHandlePwdBinding, LoginVM> {

    @Override
    protected FragmentHandlePwdBinding initDataBinding(LayoutInflater inflater, ViewGroup container) {
        return DataBindingUtil.inflate(inflater, R.layout.fragment_handle_pwd, container, false);
    }


    private boolean isUpdate = false;//是否是更改交接密码

    private String oldPwd = null;

    @Override
    public void initView() {
        super.initView();

        if (PublicValues.getUserInfo() != null) {
            KeyboardUtils.showSoftInput(getActivity());
            //没有交接密码直接设置
            if (UIUtils.isEmpty(PublicValues.getUserInfo().getHandover_password()))
            {
                binding.tvTitle.setText(R.string.pwd_handle_set);
                binding.btnSubmit.setText(R.string.dialog_ok);
                isUpdate = false;
            }
            //更改交接密码
            else
            {
                isUpdate = true;
                binding.tvTitle.setText(R.string.pwd_handle_old);
                binding.btnSubmit.setText(R.string.btn_next);
            }

            binding.btnSubmit.setOnClickListener(new OnClickCheckedUtil() {
                @Override
                public void onClicked(View view) {
                    String pwd = binding.viewPassword.getPassWord();
                    if (pwd.length() == 6) {
                        if (isUpdate) {
                            if (oldPwd == null) {
                                binding.tvTitle.setText(R.string.pwd_handle_set);
                                binding.btnSubmit.setText(R.string.dialog_ok);
                                oldPwd = pwd;
                                binding.viewPassword.clearPassword();
                                KeyboardUtils.showSoftInput(getActivity());
                            } else {
                                KeyboardUtils.hideSoftInput(getActivity());
                                LogUtils.e( "oldPwd = " + oldPwd + "  newPwd = " + pwd );
                                viewModel.modifyHandOverPassword(PublicValues.getUserInfo().getUuid(), oldPwd, pwd)
                                        .observe(getActivity(), new BaseViewObserver<LiveDataWrapper<BaseResponse>>(getActivity()) {
                                            @Override
                                            protected void onSuccess(LiveDataWrapper<BaseResponse> data) {
                                                PublicValues.getUserInfo().setHandover_password(pwd);
                                                LiveEventBus.get(AppConstants.PWD_SUCCESS, Integer.class).post(2);
                                            }
                                        });
                            }
                        } else {
                            //设置
                            viewModel.setHandOverPassword(PublicValues.getUserInfo().getUuid(), pwd)
                                    .observe(getActivity(), new BaseViewObserver<LiveDataWrapper<BaseResponse>>(getActivity()) {
                                        @Override
                                        protected void onSuccess(LiveDataWrapper<BaseResponse> data) {
                                            PublicValues.getUserInfo().setHandover_password(pwd);
                                            LiveEventBus.get(AppConstants.PWD_SUCCESS, Integer.class).post(2);
                                        }
                                    });
                        }
                    }
                }
            });
        }


    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        oldPwd = null;
    }


}
